package com.li.spark0615

import org.apache.spark.sql.{DataFrame, SaveMode, SparkSession}

object One {
  def main(args: Array[String]): Unit = {

    System.setProperty("HADOOP_USER_NAME","root")



    val session = SparkSession
      .builder()
      .appName("li")
      .master("local[*]")
      .enableHiveSupport()
      .config("hive.metastore.uris", "thrift://192.168.23.40:9083")
      .config("dfs.client.use.datanode.hostname", "true")
      .config("hive.exec.dynamic.partition.mode", "nonstrict")
      .getOrCreate()

    import session.implicits._

    session.sql("show databases").show()

    val list = List("lx_comindex")

    list.foreach(
      table =>{
        val frame: DataFrame = readMysql(session, table)

        frame
          .write
          .mode(SaveMode.Overwrite)
          .saveAsTable("ods."+table)

        print("插入成功")

      }
    )

    session.stop()

  }



  def readMysql(session:SparkSession,table:String):DataFrame={
    val reader: DataFrame = session
      .read
      .format("jdbc")
      .option("url", "jdbc:mysql://192.168.23.34:3306/laxaio?useSSL=false")
      .option("user", "root")
      .option("password", "123456")
      .option("dbtable", "( select * from  " + table + " limit 10000000 ) temp")
      .load()
    reader
  }

}
